﻿using Afterbunny.UI.WPF.Core;
using Afterbunny.Windows.Helpers;
using CygiaLog.Module;
using CygiaLog.Module.RollingFileAppender;
using CygiaUserClientModule;
using CygiaUserClientModule.Views;
using DistributedVisionRunner.App.Views;
using DistributedVisionRunner.Module;
using Prism.Events;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Unity;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows;

namespace DistributedVisionRunner.App
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        protected override Window CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            FileSystemHelper.SetupUnhandledExceptionLogging(Path.Combine(Constants.AppDataDir, "UnhandledExceptions"));
            if (DetectSameProcessExists()) return;
            base.OnStartup(e);
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.RegisterForNavigation<UserManageView>();
        }

        protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
        {
            CygiaLogModule.ConfigModule(new List<AppenderParam> { new AppenderParam("General", Path.Combine(Constants.AppDataDir, "Log")) }, "LogRegion");
            moduleCatalog.AddModule<CygiaLogModule>();

            // Log messages from DistributedVisionRunnerModule
            var ea = Container.Resolve<IEventAggregator>();
            Action<LogItem> logMethod =
                logItem =>
                ea.GetEvent<LogEvent>().Publish(("General", logItem));

            DistributedVisionRunnerModule.ConfigureModule(logMethod, "VisionRegion", true);
            DistributedVisionRunnerModule.SetDefaultImageBackground(Theme.PrimaryColor.R, Theme.PrimaryColor.G, Theme.PrimaryColor.B);
            moduleCatalog.AddModule<DistributedVisionRunnerModule>();

            moduleCatalog.AddModule<CygiaUserClientModuleModule>();
        }

        public static bool DetectSameProcessExists()
        {
            Process currentProcess = Process.GetCurrentProcess();
            var runningProcess = (from process in Process.GetProcesses()
                                  where
                                      process.Id != currentProcess.Id &&
                                      process.ProcessName.Equals(
                                          currentProcess.ProcessName,
                                          StringComparison.Ordinal)
                                  select process).FirstOrDefault();
            if (runningProcess != null)
            {
                MessageBox.Show($"An instance of {currentProcess.ProcessName} is already running.");
                Current.Shutdown();
                return true;
            }

            return false;
        }



    }
}